package com.cloudlinkscm.jedis.service;

import com.cloudlinkscm.jedis.dao.UserDao;
import com.cloudlinkscm.jedis.pojo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author : tac
 * @date : 2017/5/27
 */

@Service
public class UserService {
    private UserDao userDao;

    public UserService(@Autowired UserDao userDao){
        this.userDao = userDao;
    }

    public User selectById(String id){
        return userDao.selectByPrimaryKey(id);
    }

    public User selectByIdWithCache(String id){
        User cache = selectFromCache(id);
        if(cache == null){
            User entity = selectById(id);
            addToCache(entity);
            cache = entity;
        }
        return cache;
    }

    private void addToCache(User entity) {
    }

    private User selectFromCache(String id) {
        return null;
    }

    public String newUser(String username, String password){
        User entity = new User();
        entity.setUsername(username);
        entity.setPassword(password);
        userDao.insert(entity);
        return entity.getId();
    }
}
